#include <iostream>
#include<unordered_map>
#include<fstream>
#include"log.hpp"

const std::string filename="./dict.txt";
const std::string step=":";
extern Log lg;

// 分隔符
// apple:苹果...
// 输出型参数*
bool Split(std::string &s,std::string *part1,std::string *part2)
{
    auto pos=s.find(step);
    if(pos==s.npos) return false;
    *part1=s.substr(0,pos);
    *part2=s.substr(pos+1);
    return true;
}

class Init
{
public:
    Init()
    {
        //打开文件
        std::ifstream in(filename);
        if(in.is_open()==false)
        {
            lg(Fatal,"%s in.is_open error,",filename.c_str());
            exit(1);
        }
        // 读取文件内容并解析
        std::string line;
        while(std::getline(in,line))
        {
            std::string part1,part2;
            Split(line,&part1,&part2);
            //读完插入字典
            dict.insert({part1,part2});
        }
        // 关闭文件
    }

    //翻译工作
    std::string translation(const std::string&key)
    {
        std::cout<<"翻译开始..."<<std::endl;
        auto iter=dict.find(key);
        if(iter==dict.end()) return "Unknow";
        else
            return iter->second;
    }

    ~Init()
    {}
private:
    std::unordered_map<std::string,std::string> dict;
};